package com.example.e02;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class CalculatorActivity extends AppCompatActivity {

    private TextView display;
    private String currentInput = "";
    private double firstOperand = 0;
    private String operator = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator);

        display = findViewById(R.id.display);

        // 数字按钮
        Button btn0 = findViewById(R.id.btn0);
        Button btn1 = findViewById(R.id.btn1);
        Button btn2 = findViewById(R.id.btn2);
        Button btn3 = findViewById(R.id.btn3);
        Button btn4 = findViewById(R.id.btn4);
        Button btn5 = findViewById(R.id.btn5);
        Button btn6 = findViewById(R.id.btn6);
        Button btn7 = findViewById(R.id.btn7);
        Button btn8 = findViewById(R.id.btn8);
        Button btn9 = findViewById(R.id.btn9);

        // 操作符按钮
        Button btnPlus = findViewById(R.id.btnPlus);
        Button btnMinus = findViewById(R.id.btnMinus);
        Button btnMultiply = findViewById(R.id.btnMultiply);
        Button btnDivide = findViewById(R.id.btnDivide);

        // 其他按钮
        Button btnBackspace = findViewById(R.id.btnBackspace);
        Button btnClear = findViewById(R.id.btnClear);
        Button btnDot = findViewById(R.id.btnDot);
        Button btnEquals = findViewById(R.id.btnEquals);

        // 数字按钮点击事件
        btn0.setOnClickListener(v -> appendDigit("0"));
        btn1.setOnClickListener(v -> appendDigit("1"));
        btn2.setOnClickListener(v -> appendDigit("2"));
        btn3.setOnClickListener(v -> appendDigit("3"));
        btn4.setOnClickListener(v -> appendDigit("4"));
        btn5.setOnClickListener(v -> appendDigit("5"));
        btn6.setOnClickListener(v -> appendDigit("6"));
        btn7.setOnClickListener(v -> appendDigit("7"));
        btn8.setOnClickListener(v -> appendDigit("8"));
        btn9.setOnClickListener(v -> appendDigit("9"));

        // 小数点按钮
        btnDot.setOnClickListener(v -> appendDot());

        // 操作符按钮
        btnPlus.setOnClickListener(v -> setOperator("+"));
        btnMinus.setOnClickListener(v -> setOperator("-"));
        btnMultiply.setOnClickListener(v -> setOperator("*"));
        btnDivide.setOnClickListener(v -> setOperator("/"));

        // 回退按钮
        btnBackspace.setOnClickListener(v -> backspace());

        // 清空按钮
        btnClear.setOnClickListener(v -> clear());

        // 等号按钮
        btnEquals.setOnClickListener(v -> calculate());
    }

    // 追加数字到显示区域
    private void appendDigit(String digit) {
        currentInput += digit;
        display.setText(currentInput);
    }

    // 追加小数点
    private void appendDot() {
        if (!currentInput.contains(".")) {
            currentInput += ".";
            display.setText(currentInput);
        }
    }

    // 设置操作符
    private void setOperator(String op) {
        if (!currentInput.isEmpty()) {
            firstOperand = Double.parseDouble(currentInput);
            operator = op;
            currentInput = "";
        }
    }

    // 回退功能
    private void backspace() {
        if (currentInput.length() > 0) {
            currentInput = currentInput.substring(0, currentInput.length() - 1);
            display.setText(currentInput.isEmpty() ? "0" : currentInput);
        }
    }

    // 清空功能
    private void clear() {
        currentInput = "";
        firstOperand = 0;
        operator = "";
        display.setText("0");
    }

    // 计算功能
    private void calculate() {
        if (!currentInput.isEmpty() && !operator.isEmpty()) {
            double secondOperand = Double.parseDouble(currentInput);
            double result = 0;

            switch (operator) {
                case "+":
                    result = firstOperand + secondOperand;
                    break;
                case "-":
                    result = firstOperand - secondOperand;
                    break;
                case "*":
                    result = firstOperand * secondOperand;
                    break;
                case "/":
                    if (secondOperand != 0) {
                        result = firstOperand / secondOperand;
                    } else {
                        display.setText("Error");
                        return;
                    }
                    break;
            }

            currentInput = String.valueOf(result);
            display.setText(currentInput);
            operator = "";
        }
    }
}